/**
 * 
 */
package goodroommate.service.impl;

import java.sql.Date;
import java.util.ArrayList;

import goodroommate.dal.DatabaseSession;
import goodroommate.dao.IUserDetailDao;
import goodroommate.dao.impl.UserDetailDao;
import goodroommate.domain.Nation;
import goodroommate.domain.University;
import goodroommate.domain.UserDetail;
import goodroommate.exception.DatabaseException;
import goodroommate.exception.ItemAlreadyExistsException;
import goodroommate.exception.ItemNotExistsException;
import goodroommate.service.IUserDetailService;

/**
 * @author ZhouWuAi
 *
 */
public class UserDetailService implements IUserDetailService {

	IUserDetailDao userDetailDao=new UserDetailDao();
	/* (non-Javadoc)
	 * @see goodroommate.service.IUserDetailService#addUserDetail(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean addUserDetail(long userID, String realName, String gender, Date birthday,String nation,String belief, String phoneNumber,
			String qq, String email, String address, long university, String major,String domitory)
			throws DatabaseException, ItemAlreadyExistsException {
		
		try(DatabaseSession dbs=new DatabaseSession()){
			if(userDetailDao.getUserDetailByUserId(dbs, userID)!=null){
				throw new ItemAlreadyExistsException();
			}
			else{
				UserDetail userDetail=new UserDetail();
				userDetail.setUserid(userID);
				userDetail.setRealname(realName);
				userDetail.setGender(gender);
				userDetail.setBirthday(birthday);
				userDetail.setNation(nation);
				userDetail.setBelief(belief);
				userDetail.setTelphone(phoneNumber);
				userDetail.setQq(qq);
				userDetail.setEmail(email);
				userDetail.setAddress(address);
				userDetail.setUniversity(university);
				userDetail.setMajor(major);
				userDetail.setDomitory(domitory);
				userDetailDao.addUserDetail(dbs, userDetail);
				return true;
			}
		}
	}
	@Override
	public UserDetail getUserDetailByUserID(long userID) throws DatabaseException, ItemNotExistsException {
		try(DatabaseSession dbs=new DatabaseSession()){
			UserDetail userDetail=userDetailDao.getUserDetailByUserId(dbs, userID);
			if(userDetail==null){
				throw new ItemNotExistsException();
			}
			return userDetail;
			
		}catch(DatabaseException e){
			e.printStackTrace();
			throw new DatabaseException(e);
		}

	}
	@Override
	public boolean updateUserDetail(long userID, String realName, String gender,String belief,String nation, Date birthday, String phoneNumber,
			String qq, String email, String address, long university, String major,String domitory)
			throws DatabaseException, ItemNotExistsException {
		
		
		try(DatabaseSession dbs=new DatabaseSession()){
			
			UserDetail userDetail=userDetailDao.getUserDetailByUserId(dbs, userID);
			if(userDetail==null){
				throw new ItemNotExistsException();
			}
			else{
				userDetail.setUserid(userID);
				userDetail.setRealname(realName);
				userDetail.setGender(gender);
				userDetail.setBelief(belief);
				userDetail.setNation(nation);
				userDetail.setBirthday(birthday);
				userDetail.setTelphone(phoneNumber);
				userDetail.setQq(qq);
				userDetail.setEmail(email);
				userDetail.setAddress(address);
				userDetail.setUniversity(university);
				userDetail.setMajor(major);
				userDetail.setDomitory(domitory);
				userDetailDao.updateUserDetail(dbs, userDetail);
				return true;
			}
		}
	}
	@Override
	public ArrayList<Nation> getAllNations() throws DatabaseException, ItemNotExistsException {
		
		try(DatabaseSession dbs=new DatabaseSession()){
			
			ArrayList<Nation> rst=userDetailDao.getAllNations(dbs);
			if(rst.size()==0){
				throw new ItemNotExistsException();
			}
			return rst;
		}
	}
	@Override
	public Nation getNationByName(String name) throws DatabaseException {
		try(DatabaseSession dbs=new DatabaseSession()){
			Nation nation=userDetailDao.getNationByName(name, dbs);
			return nation;
		}
	}
	@Override
	public ArrayList<University> getAllUniversity() throws DatabaseException, ItemNotExistsException {
		
		try(DatabaseSession dbs=new DatabaseSession()){
			
			ArrayList<University> rst=userDetailDao.getAllUniversity(dbs);
			if(rst.size()==0){
				throw new ItemNotExistsException();
			}
			return rst;
		}
	}
	
	
	
	

	
}
